import Cookies from 'js-cookie'
// js-cookie 的使用

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true, // 默认是 1 即true
    withoutAnimation: false
  },
  device: 'desktop'
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    console.log("TOGGLE_SIDEBAR")
    state.sidebar.opened = !state.sidebar.opened  // 取反
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1) // 开
    } else {
      Cookies.set('sidebarStatus', 0)// 关
    }
  },

  CLOSE_SIDEBAR: (state, withoutAnimation) => { // 关
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },

  TOGGLE_DEVICE: (state, device) => { // 更换设备
    state.device = device
  }
}

const actions = {
  //改变sidebar
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  // 关闭sidebar
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  // 改变device
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
